﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HML.Demo
{
    public partial class TimerPrecisionForm : ItemFormTemplate
    {
        public TimerPrecisionForm()
        {
            InitializeComponent();
            this.Load += new System.EventHandler(this.Form_Load);
            this.Shown += new System.EventHandler(this.Form_Shown);
        }

        #region 实现

        public override ItemFormTemplateViewObject AsynchRequestTask(ItemFormTemplateViewObject view)
        {
            System.Threading.Thread.Sleep(300);
            view.ViewData = DemoHelper.GetDescription(AppDomain.CurrentDomain.BaseDirectory + "例子\\控件\\TimerPrecisionForm\\TimerPrecisionForm.rtf");
            return view;
        }

        public override void DataBind(ItemFormTemplateViewObject viewObject)
        {
            this.introduceBrowser.Rtf = viewObject.ViewData.ToString();
        }

        public override void ShowView()
        {
            base.ShowView();
        }

        public override void HideView()
        {
            base.HideView();
        }

        public override void CloseView()
        {
            base.CloseView();
        }

        #endregion

        #region 属性面板

        private int count = 0;
        private Stopwatch sw = new Stopwatch();

        private void Form_Load(object sender, EventArgs e)
        {
            this.controlContainer.SizeChanged += this.ControlContainer_SizeChanged;

            this.demo_control.Tick += this.Demo_control_Tick;

            this.status_label.Text = "";
            this.count_numericUpDown.Value = 100;
            this.internal_numericUpDown.Value = 10;
            this.start_buttonPlus.Click += this.Start_buttonPlus_Click;
        }

        private void Form_Shown(object sender, EventArgs e)
        {
            this.splitContainer.SplitterDistance = Math.Max(this.splitContainer.Width / 4, this.splitContainer.Width - MainForm.GlobalSplitWidth);
            this.splitContainer.SplitterMoved += this.SplitContainer_SplitterMoved;

            MainForm.LoadAssistTabControl(this.assistTabControl);
        }

        private void SplitContainer_SplitterMoved(object sender, SplitterEventArgs e)
        {
            SplitContainer control = (SplitContainer)sender;
            MainForm.GlobalSplitWidth = this.splitContainer.Width - control.SplitterDistance;
        }

        private void ControlContainer_SizeChanged(object sender, EventArgs e)
        {
            Panel control = (Panel)sender;
            this.status_label.Location = new Point((control.Width - this.status_label.Width) / 2, (control.Height - this.status_label.Height) / 2);
        }

        private void Demo_control_Tick(object sender, EventArgs e)
        {
            uint countsum = (uint)this.count_numericUpDown.Value;
            this.count++;

            if (count == countsum)
            {
                sw.Stop();
                this.demo_control.Enabled = false;
                this.Invoke(
                    new Action(delegate
                    {
                        this.status_label.Text = String.Format(@"
预计耗时：{0}毫秒
实际耗时：{1}毫秒", (this.count_numericUpDown.Value * this.internal_numericUpDown.Value).ToString("N0"), sw.ElapsedMilliseconds.ToString("N0"));
                    })
                    );
            }
            this.status_label.BackColor = (count % 2 == 0) ? Color.Red : Color.Blue;
        }

        private void Start_buttonPlus_Click(object sender, EventArgs e)
        {
            count = 0;
            sw.Start();
            this.demo_control.Enabled = true;
        }

        #endregion

    }
}
